<h1 id="name">NAME</h1>

<p>Joose.Manual.Construction - Object construction with Joose</p>

<h1 id="wherestheconstructor">WHERE'S THE CONSTRUCTOR?</h1>

<p>You do not need to define a constructor method for your classes. Really. Instead, define an <code>initialize</code> method. </p>

<p>When you build a class using Joose it will be managed by the instance of Joose.Meta.Class. This instance (a <em>metaclass instance</em>) provides a constructor for you. 
This constructor will perform attributes initialization and then will call a <code>initialize</code> method with the same arguments. </p>

<p>If you inherit from parent class, you need to call the inherited <code>initialize</code>, however usually you should just define an <code>after</code> modifier:</p>

<pre><code>    Class('User', {
        isa : Person,

        after : {
            initialize : function () {
            } 
        }
    })
</code></pre>

<p>If you are still sure you need a custom constructor you may provide it using a <code>constructor</code> builder:</p>

<pre><code>    Class('User', {

        constructor : function (i, really, know, what, iam, doing) {
        }

        ....
    })
</code></pre>

<h1 id="objectconstructionandattributes">OBJECT CONSTRUCTION AND ATTRIBUTES</h1>

<p>The Joose-provided constructor accepts an object with properties, matching your attributes. This is just another way in which Joose keeps you from worrying how classes are implemented. 
Simply define a class and you're ready to start creating objects!</p>

<pre><code>    Class('User', {
        isa : Person,

        has : {
            password : { init : '12345' },
            lastLogin : { init : function () { return new Date() } }
        }
    })

    var user = new User({
        password : 'abcdef',
        lastLogin : 'Yesterday'
    })
</code></pre>

<p>If, during construction, you'll provide a function as an initializing value, it will be assigned as-is (not will be called).</p>

<h1 id="namespaces">NAMESPACES</h1>

<p>In Joose, each class also act a namespace with the same name. </p>

<p>The name of the class can consist from one or several <em>namespace segments</em>. The separator symbol is a dot: <code>.</code> 
After declaration, Joose translates the class's name to constructor and place it into appropriate namespace. For example:</p>

<pre><code>    Class("MyApp.Point", {
        has: {
            x: {is: "ro"},
            y: {is: "rw"},
        }
    })

    Class("MyApp.Point.ThreeD", {
        isa: Point,

        has: {
            z: {}
        }
    })

    Class("MyApp", {
        has: {
            name: null
        }
    })

    var point = new MyApp.Point()
    var point3d = new MyApp.Point.ThreeD()
    var myapp = new MyApp()
</code></pre>

<p>Note, how <code>MyApp</code> class was created already after its namespace segment was declared.</p>

<h1 id="bodybuilder"><code>body</code> BUILDER</h1>

<p>Each class also support special builder <code>body</code>, which should be a function. </p>

<pre><code>    Class("MyApp.Point.ThreeD", {
        isa: Point,

        has: {
            z: {}
        },

        body : function (myAppPoint3D) {
            console.log(myAppPoint3D == MyApp.Point.ThreeD) //prints 'true'

            console.log(this == MyApp.Point.ThreeD) //prints 'true'

            ....

            var private = new MyApp.Point.ThreeD({ x: 1, y : 2, z: 3})

            ....

            MyApp.Point.ThreeD.meta.extend({
                methods : {
                }
            })
        }
    })
</code></pre>

<p>This function will be called right after class construction, with the class's constructor as 1st argument and in the same scope.</p>

<p>The class will be already fully constructed at the time of <code>body</code> execution, you can create its instances or even <em>extend</em> it with some additional
methods (see <a href="Mutability.html">Joose.Manual.Mutability</a> for details)</p>

<p>You may declare new classes (or roles) in the <code>body</code>. Such class's constructor will be placed in the namespace of the outer class.  </p>

<pre><code>    Class("MyApp.Point", {

        body : function () {

            Class("ThreeD", {
                isa: MyApp.Point,

                has: {
                    z: {}
                }
            }
        }
    })

    var point = new MyApp.Point()
    var point3d = new MyApp.Point.ThreeD()
</code></pre>

<p><code>body</code> builder is a correct place to perform some action after creation of class, because in general case, the creation may be an asynchronous (for dependency loading for example).</p>

<h1 id="modules">MODULES</h1>

<p>Modules are classes, which consists from the <code>body</code> only. Modules may not be instantiated, however, its possible to "promote" Module to Class.
<code>Module</code> helper also can be called with <code>body</code> as 2nd argument directly: </p>

<pre><code>    Module("MyApp", function () {

        Module("Point", function () {

            Class("ThreeD", {
                isa: Point,

                has: {
                    z: {}
                }
            }
        })
    })

    var myapp = new MyApp() //exception

    var point3d = new MyApp.Point.ThreeD() //ok

    ...

    //promoting Module to class
    Class("MyApp", {
        ...
    })
    var myapp = new MyApp() //ok
</code></pre>

<h1 id="author">AUTHOR</h1>

<p>Nickolay Platonov <a href="mailto:nickolay8@gmail.com">nickolay8@gmail.com</a></p>

<p>Heavily based on the original content of Moose::Manual, by Dave Rolsky <a href="mailto:autarch@urth.org">autarch@urth.org</a></p>

<h1 id="copyrightandlicense">COPYRIGHT AND LICENSE</h1>

<p>Copyright (c) 2008-2009, Malte Ubl, Nickolay Platonov</p>

<p>All rights reserved.</p>

<p>Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:</p>

<ul>
<li>Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.</li>
<li>Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.</li>
<li>Neither the name of Malte Ubl nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. </li>
</ul>

<p>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. </p>
